package cc.springsecurity.auth2.client.token;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@RestController
public class SecurityAuth2ClientTokenApplication {

	@Autowired
	private RedisConnectionFactory redisConnectionFactory;
	
	public static void main(String[] args) {
		SpringApplication.run(SecurityAuth2ClientTokenApplication.class, args);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping({ "/user", "/me" })
	public Object user(OAuth2Authentication authentication) {
		RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
		OAuth2AccessToken access_token = redisTokenStore.getAccessToken(authentication);
		DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter();
		Map<String, Object> rs = (Map<String, Object>) converter.convertAccessToken(access_token, authentication);
		return rs;
	}
}
